package com.bestom.bluetoothlock.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.content.Context;


import com.bestom.bluetoothlock.util.LogMsg;

import java.util.UUID;

public class BluetoothManage {
    private static final String UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR = "00002902-0000-1000-8000-00805f9b34fb";
    /* access modifiers changed from: private */
    public UUID NOTIFI_UUID = null;
    /* access modifiers changed from: private */
    public UUID SERVICE_UUID = null;
    private UUID WRITE_UUID = null;
    private Context context;
    private BluetoothAdapter mBTAdapter;
    /* access modifiers changed from: private */
    public BluetoothGatt mBTGatt;
    private BluetoothGattCallback mBTGattCallback = new BluetoothGattCallback() {
        public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
            super.onCharacteristicRead(bluetoothGatt, bluetoothGattCharacteristic, i);
        }

        public void onCharacteristicWrite(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
            super.onCharacteristicWrite(bluetoothGatt, bluetoothGattCharacteristic, i);
            LogMsg.Mess("onCharacteristicWrite status:" + i);
            BluetoothManage.this.mOnBluetoothListers.onCharacteristicWrite(bluetoothGatt, bluetoothGattCharacteristic, i);
        }

        public void onCharacteristicChanged(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic) {
            super.onCharacteristicChanged(bluetoothGatt, bluetoothGattCharacteristic);
            BluetoothManage.this.mOnBluetoothListers.ReadByte(bluetoothGattCharacteristic.getValue());
        }

        public void onPhyUpdate(BluetoothGatt bluetoothGatt, int i, int i2, int i3) {
            super.onPhyUpdate(bluetoothGatt, i, i2, i3);
        }

        public void onPhyRead(BluetoothGatt bluetoothGatt, int i, int i2, int i3) {
            super.onPhyRead(bluetoothGatt, i, i2, i3);
        }

        public void onConnectionStateChange(BluetoothGatt bluetoothGatt, int i, int i2) {
            super.onConnectionStateChange(bluetoothGatt, i, i2);
            LogMsg.Mess("onConnectionStateChange status=" + i + " newState=" + i2);
            if (i < 100) {
                int unused = BluetoothManage.this.state = i2;
            } else {
                int unused2 = BluetoothManage.this.state = 0;
            }
            if (i == 133 || i == 129 || i == 257) {
                BluetoothManage.this.mBTGatt.close();
                BluetoothManage.this.mOnBluetoothListers.ConnectionStateChange(i, 133);
            } else if (i2 == 2) {
                BluetoothManage.this.mOnBluetoothListers.ConnectionStateChange(i, i2);
                BluetoothManage.this.mBTGatt.discoverServices();
                LogMsg.Mess("连接成功");
            } else if (i2 == 0) {
                BluetoothManage.this.mOnBluetoothListers.ConnectionStateChange(i, i2);
                BluetoothManage.this.close();
                LogMsg.Mess("断开连接");
            }
        }

        public void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i) {
            super.onServicesDiscovered(bluetoothGatt, i);
            LogMsg.Mess("onServicesDiscovered");
            LogMsg.Mess("status:" + i);
            for (BluetoothGattService uuid : bluetoothGatt.getServices()) {
                LogMsg.Mess("BluetoothGattService uuid:" + uuid.getUuid().toString());
            }
            if (BluetoothManage.this.SERVICE_UUID == null || BluetoothManage.this.NOTIFI_UUID == null) {
                LogMsg.Mess("SERVICE_UUID == null 或 NOTIFI_UUID == null");
                return;
            }
            BluetoothManage bluetoothManage = BluetoothManage.this;
            bluetoothManage.enableNoti(bluetoothManage.NOTIFI_UUID, BluetoothManage.this.SERVICE_UUID);
        }

        public void onDescriptorRead(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i) {
            super.onDescriptorRead(bluetoothGatt, bluetoothGattDescriptor, i);
        }

        public void onDescriptorWrite(BluetoothGatt bluetoothGatt, BluetoothGattDescriptor bluetoothGattDescriptor, int i) {
            super.onDescriptorWrite(bluetoothGatt, bluetoothGattDescriptor, i);
        }

        public void onReliableWriteCompleted(BluetoothGatt bluetoothGatt, int i) {
            super.onReliableWriteCompleted(bluetoothGatt, i);
        }

        public void onReadRemoteRssi(BluetoothGatt bluetoothGatt, int i, int i2) {
            super.onReadRemoteRssi(bluetoothGatt, i, i2);
            BluetoothManage.this.mOnBluetoothListers.GetRssi(i);
        }

        public void onMtuChanged(BluetoothGatt bluetoothGatt, int i, int i2) {
            super.onMtuChanged(bluetoothGatt, i, i2);
        }
    };
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
        public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
            BluetoothManage.this.mOnBluetoothListers.SearchToDevice(bluetoothDevice, bArr, i);
        }
    };
    public OnBluetoothListers mOnBluetoothListers;
    /* access modifiers changed from: private */
    public int state = 0;

    public interface OnBluetoothListers {
        void ConnectionStateChange(int i, int i2);

        void GetRssi(int i);

        void ReadByte(byte[] bArr);

        void SearchToDevice(BluetoothDevice bluetoothDevice, byte[] bArr, int i);

        void notifySuccess();

        void onCharacteristicWrite(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i);

        void onServicesDiscovered(BluetoothGatt bluetoothGatt, int i);
    }

    public BluetoothManage(Context context2) {
        this.context = context2;
        this.mBTAdapter = BluetoothAdapter.getDefaultAdapter();
    }

    public void setUUID(String str, String str2, String str3) {
        this.SERVICE_UUID = UUID.fromString(str);
        this.WRITE_UUID = UUID.fromString(str2);
        this.NOTIFI_UUID = UUID.fromString(str3);
    }

    public boolean enableNoti(UUID uuid, UUID uuid2) {
        LogMsg.Mess("enableLostNoti");
        BluetoothGattService service = this.mBTGatt.getService(uuid2);
        if (service == null) {
            LogMsg.Mess("TxPowerService == null");
            return false;
        }
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(uuid);
        if (characteristic != null) {
            return setCharacteristicNotification(characteristic, true);
        }
        LogMsg.Mess("TxPowerLevel == null");
        return false;
    }

    private boolean setCharacteristicNotification(BluetoothGattCharacteristic bluetoothGattCharacteristic, boolean z) {
        BluetoothGatt bluetoothGatt;
        boolean z2 = false;
        if (this.mBTAdapter == null || (bluetoothGatt = this.mBTGatt) == null) {
            LogMsg.Mess("BluetoothAdapter not initialized");
            return false;
        }
        bluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, z);
        BluetoothGattDescriptor descriptor = bluetoothGattCharacteristic.getDescriptor(UUID.fromString(UUID_CLIENT_CHARACTERISTIC_CONFIG_DESCRIPTOR));
        LogMsg.Mess("writeDescriptor");
        if (descriptor != null) {
            if ((bluetoothGattCharacteristic.getProperties() & 16) != 0) {
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            } else if ((bluetoothGattCharacteristic.getProperties() & 32) != 0) {
                descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
            }
            z2 = this.mBTGatt.writeDescriptor(descriptor);
            LogMsg.Mess("通知服务使能成功" + z2);
            if (z2) {
                this.mOnBluetoothListers.notifySuccess();
            }
        } else {
            LogMsg.Mess("通知服务使能失败");
        }
        return z2;
    }

    public void connect(BluetoothDevice bluetoothDevice) {
        if (bluetoothDevice != null) {
            BluetoothDevice remoteDevice = this.mBTAdapter.getRemoteDevice(bluetoothDevice.getAddress());
            BluetoothGatt bluetoothGatt = this.mBTGatt;
            if (bluetoothGatt != null) {
                bluetoothGatt.disconnect();
            }
            this.mBTGatt = remoteDevice.connectGatt(this.context, false, this.mBTGattCallback);
            if (this.mBTGatt != null) {
                LogMsg.Mess("连接成功mBTGatt:" + this.mBTGatt);
            }
        }
    }

    public void connect(String str) {
        if (str != null) {
            this.mBTGatt = this.mBTAdapter.getRemoteDevice(str).connectGatt(this.context, false, this.mBTGattCallback);
            LogMsg.Mess("连接成功mBTGatt:" + this.mBTGatt + "\naddress:" + str);
        }
    }

    public void disconnect() {
        BluetoothGatt bluetoothGatt = this.mBTGatt;
        if (bluetoothGatt != null) {
            bluetoothGatt.disconnect();
            LogMsg.Mess("mBTGatt disconnect");
        }
    }

    public void close() {
        BluetoothGatt bluetoothGatt = this.mBTGatt;
        if (bluetoothGatt != null) {
            bluetoothGatt.close();
            LogMsg.Mess("mBTGatt closed");
        }
    }

    public void StartSearchDevice() {
        LogMsg.Mess("StartSearchDevice");
        if (this.mBTAdapter != null && this.mLeScanCallback != null) {
            EndSearchDevice();
            this.mBTAdapter.startLeScan(this.mLeScanCallback);
        }
    }

    public void EndSearchDevice() {
        BluetoothAdapter.LeScanCallback leScanCallback;
        LogMsg.Mess("EndSearchDevice");
        BluetoothAdapter bluetoothAdapter = this.mBTAdapter;
        if (bluetoothAdapter != null && (leScanCallback = this.mLeScanCallback) != null) {
            bluetoothAdapter.stopLeScan(leScanCallback);
        }
    }

    public boolean write(byte[] bArr, String str, String str2) {
        String str3;
        BluetoothGatt bluetoothGatt = this.mBTGatt;
        if (bluetoothGatt == null) {
            LogMsg.Mess("mBTGatt == null 发送失败！");
            return false;
        }
        BluetoothGattService service = bluetoothGatt.getService(UUID.fromString(str2));
        if (service == null) {
            LogMsg.Mess("localBluetoothGattService == null 发送失败！");
            return false;
        }
        BluetoothGattCharacteristic characteristic = service.getCharacteristic(UUID.fromString(str));
        characteristic.setValue(bArr);
        characteristic.setWriteType(1);
        if (characteristic == null) {
            LogMsg.Mess("characteristic == null  发送失败！");
            return false;
        } else if (characteristic.getValue() == null) {
            LogMsg.Mess("write value == null");
            return false;
        } else {
            boolean writeCharacteristic = this.mBTGatt.writeCharacteristic(characteristic);
            if (writeCharacteristic) {
                str3 = "发送成功:" + bytesToHexString(bArr);
            } else {
                str3 = "发送失败";
            }
            LogMsg.Mess(str3);
            return writeCharacteristic;
        }
    }

    private String bytesToHexString(byte[] bArr) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < bArr.length; i++) {
            if (i % 32 == 0 && i != 0) {
                stringBuffer.append(" \n ");
            }
            String hexString = Integer.toHexString(bArr[i]);
            if (hexString.length() < 2) {
                hexString = "0" + hexString;
            }
            if (hexString.length() > 2) {
                hexString = hexString.substring(hexString.length() - 2);
            }
            stringBuffer.append("" + hexString);
        }
        return stringBuffer.toString();
    }

    public boolean checkBLEenable() {
        BluetoothAdapter bluetoothAdapter = this.mBTAdapter;
        if (bluetoothAdapter != null) {
            return bluetoothAdapter.isEnabled();
        }
        return false;
    }

    public boolean getState() {
        return this.state == 2;
    }

    public void setOnBluetoothListens(OnBluetoothListers onBluetoothListers) {
        this.mOnBluetoothListers = onBluetoothListers;
    }
}
